//
//  ViewController.m
//  18-HomeWork-添加联系人
//
//  Created by dllo on 16/8/8.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ViewController.h"
#import "CustomTableViewCell.h"
#import "Contacts.h"
#import "InformationViewController.h"
#import "AddContactViewController.h"
#import "SQLiteDataBaseManager.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, SaveContactDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *addContactButton;
@property (nonatomic, strong) NSMutableArray *arrayOfData;
@property (nonatomic, strong) NSString *contactPath;
@property (nonatomic, strong) NSMutableArray *arrayOfTitle;
@property (nonatomic, strong) NSMutableSet *set;
@property (nonatomic, strong) SQLiteDataBaseManager *dataBaseManager;
@property (nonatomic, strong) NSMutableArray *studentArray;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    _dataBaseManager = [SQLiteDataBaseManager sharedManager];
    [_dataBaseManager openSQLite];
    [self getStudentData];
    
}

- (void)getStudentData {
    self.studentArray = [NSMutableArray array];
    _studentArray = [_dataBaseManager selectAll];
    if (_studentArray == nil) {
        [_dataBaseManager createTable];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_studentArray) {
        return _studentArray.count;
    }
    return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    if (_studentArray) {
        Contacts *con = [_studentArray objectAtIndex:indexPath.row];
        cell.labelOfName.text = con.nameString;
        NSString *avatarsDirectory = [self getAvatarDirectory];
        NSString *imagePath = [avatarsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", con.nameString]];
        NSLog(@"afafafaa:%@", imagePath);
        NSData *data = [NSData dataWithContentsOfFile:imagePath];
        UIImage *image = [UIImage imageWithData:data];
        cell.headImageView.image = image;
    }
    cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0f green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1];
    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    [_dataBaseManager createTable];
    if ([sender isKindOfClass:[CustomTableViewCell class]]) {
        CustomTableViewCell *cell = sender;
        InformationViewController *information = segue.destinationViewController;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        information.contact = [_studentArray objectAtIndex:indexPath.row];
    } else {
        AddContactViewController *addVC = segue.destinationViewController;
        addVC.delegate = self;
    }
}
// 传值协议方法
- (void)addStudent:(Contacts *)con {
    if (con.nameString.length > 0) {
        [_dataBaseManager insertCon:con];
        [self getStudentData];
        [self.tableView reloadData];
        [_dataBaseManager closeSQLite];
    }
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_dataBaseManager openSQLite];
        Contacts *con = [_studentArray objectAtIndex:indexPath.row];
        [_studentArray removeObjectAtIndex:indexPath.row];
        [_dataBaseManager deleteCon:con];
        NSFileManager *manager = [NSFileManager defaultManager];
        NSString *avatarsDirectory = [self getAvatarDirectory];
        NSString *imagePath = [avatarsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", con.nameString]];
        [manager removeItemAtPath:imagePath error:nil];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [_dataBaseManager closeSQLite];
    }
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}

- (NSString *)getAvatarDirectory {
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *avatarsDirectory = [[pathArray firstObject] stringByAppendingPathComponent:@"avatarsDirectory"];
    return avatarsDirectory;
}


@end
